package Products;

import java.util.Map;
import java.util.TreeMap;

import org.joda.time.DateTime;

import Exceptions.HistoricalInformationException;
import Exceptions.ProductNeverPurchasedException;
import Exceptions.ProductSalesPriceNeverSetException;
import Magnitudes.Magnitude;
import Magnitudes.Money;
import Magnitudes.Percentage;
import Magnitudes.Pesos;

public class Presentation implements Comparable{
	
	private Magnitude presentation;
	private String barCode;
	private Money unitPurchasePrice;
	private Money unitSalesPrice;
	private HistoricalMapDateMoney historicalUnitPurchasePrice;
	private HistoricalMapDateMoney historicalUnitSalesPrice;
	
	
/*
	public Presentation(Kilogram kilo, String code) {
		this.presentation = kilo;
		this.barCode = code;
	}
	*/
	
	public Presentation(Magnitude magnitude, String code) {
		this.presentation = magnitude;
		this.barCode = code;
		this.historicalUnitPurchasePrice = new HistoricalMapDateMoney();
		this.historicalUnitSalesPrice = new HistoricalMapDateMoney();
		//this.historicalUnitPurchasePrice = new TreeMap<DateTime, Money>();
		//this.historicalUnitSalesPrice = new TreeMap<DateTime, Money>();
	}

	public Boolean equals(Presentation anotherPresentation){
		return this.getBarcode().equals(anotherPresentation.getBarcode());
	}

	public String getBarcode() {
		return this.barCode;
	}


	public Magnitude getPresentation(){
		return this.presentation;
	}
	
	
	public HistoricalMapDateMoney getHistoricalUnitPurchasePrice(){
		return this.historicalUnitPurchasePrice;
	}
	
	
	public HistoricalMapDateMoney getHistoricalUnitSalesPrice(){
		return this.historicalUnitSalesPrice;
	}
	
	
	public String printPresentationMagnitude() {
		return getPresentation().toString();
	}


	public Money getUnitPurchasePrice() throws ProductNeverPurchasedException {
		if(this.unitPurchasePrice == null){
			throw new ProductNeverPurchasedException();
		}else{
			return this.unitPurchasePrice;
		}				
	}


	public void updateUnitPurchasePriceTo(Money amount) {
		this.unitPurchasePrice = amount;
		this.historicalUnitPurchasePrice.updatePriceTo(amount);
	}


	public Money getUnitSalesPrice() throws ProductSalesPriceNeverSetException {  
		if(this.unitSalesPrice == null){
			throw new ProductSalesPriceNeverSetException();
		}else{
			return this.unitSalesPrice;
		}		 
	}


	public void updateUnitSalesPriceTo(Money amount) {
		this.unitSalesPrice = amount;
		this.historicalUnitSalesPrice.updatePriceTo(amount);
	}


	public void updateUnitPurchasePriceBy(Percentage percentage) throws ProductNeverPurchasedException {
		//double total = this.getUnitPurchasePrice().amount();
		//total = total + (total * percentage.amount()) / 100;
		this.updateUnitPurchasePriceTo(((Pesos) this.getUnitPurchasePrice()).addPercentage(percentage));
		//this.updateUnitPurchasePriceTo(new Pesos(total));	
	}

	
	
	public void updateSalesPriceBy(Percentage percentage) throws ProductSalesPriceNeverSetException{
		double total = this.getUnitSalesPrice().amount();
		total = total + (total * percentage.amount()) / 100;
		this.updateUnitSalesPriceTo(new Pesos(total));
	}
	
		

	public void updateUnitPurchasePriceByAndUpdateSalesPrice(Percentage purchasePercentage, Percentage salesPercentage) throws ProductNeverPurchasedException, ProductSalesPriceNeverSetException {
		this.updateUnitPurchasePriceBy(purchasePercentage);
		this.updateSalesPriceBy(salesPercentage);
	}


/*
	public void salesPriceAt(DateTime today) throws HistoricalInformationException {
		Money money = this.getHistoricalUnitSalesPrice().get(today);
		if(money == null){
			throw new HistoricalInformationException();
		}else{
			this.updateUnitSalesPriceTo(money);
		}		
	}


	public void purchasePriceAt(DateTime today) throws HistoricalInformationException {
		Money money = this.getHistoricalUnitPurchasePrice().get(today);
		if(money == null){
			throw new HistoricalInformationException();
		}else{
			this.updateUnitPurchasePriceTo(money);
		}
	}
*/

	public Money salesPriceAt(DateTime today) throws HistoricalInformationException {
		Money money = this.getHistoricalUnitSalesPrice().priceAt(today);
		return money;				
	}
	
	
	public Money purchasePriceAt(DateTime today) throws HistoricalInformationException {
		Money money = this.getHistoricalUnitPurchasePrice().priceAt(today);
		return money;		
	}

	public int compareTo(Presentation another) {
		int retVal = -1;
		if (this.equals(another)){
			retVal = 0;
		}
		return retVal;
	}

	@Override
	public int compareTo(Object arg0) {
		int retVal = -1;
		if (this.equals(arg0)){
			retVal = 0;
		}
		return retVal;
	}
	
	
	
	

}
